package com.itheima.chapter10.producerconsumer11;

//消费者
public class Consumer extends Thread{
    private Share s;
    public Consumer(Share s){
        this.s =s;
    }
    @Override
    public void run() {
        while (true) {
            synchronized (s.getLock()) {
                if (s.getCount()==0){
                    break;
                }else{
                    if (s.isFlag()){
                        System.out.println("消费者使用资源");
                        s.setFlag(false);
                        s.getLock().notifyAll();
                        s.setCount(s.getCount()-1);
                    }else {
                        try {
                            s.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

    }
}
